package pers.course.exam;

import java.util.*;

class Account {
    protected double balance;

    public Account(double init_balance) {
        balance = init_balance;
    }

    public double getBalance() {
        return balance;
    }

    public boolean deposit(double amt) {
        balance += amt;
        return true;
    }

    public boolean withdraw(double amt) {
        if (balance >= amt) {
            balance -= amt;
            return true;
        } else {
            return false;
        }
    }
}

class CheckingAccount extends Account {
    private double overdraftProtection;

    public CheckingAccount(double balance) {
        super(balance);

    }

    public CheckingAccount(double balance, double protect) {
        super(balance);
        this.overdraftProtection = protect;
    }


    public double getOverdraftProtection() {
        return overdraftProtection;
    }

    public void setOverdraftProtection(double overdraftProtection) {
        this.overdraftProtection = overdraftProtection;
    }

    public boolean withdraw(double amt) {
        if (balance >= amt) {
            balance -= amt;
            //return true;
        } else if (overdraftProtection >= amt - balance) {
            overdraftProtection -= (amt - balance);
            balance = 0;
            //return true;
        } else {
            return false;
        }
        return true;
    }
}

class SavingsAccount extends Account {
    private double interestRate;

    public SavingsAccount(double balance, double interest_rate) {
        super(balance);
        this.interestRate = interest_rate;

    }

    public double getInterestRate() {
        return interestRate;
    }

    public void setInterestRate(double interestRate) {
        this.interestRate = interestRate;
    }
}

class Customer {
    private String firstName;
    private String lastName;
    private Account account;

    public Customer(String f, String l) {
        firstName = f;
        lastName = l;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account acct) {//多态性，可将子类对象赋值
        this.account = acct;
    }

}

class Bank {
    private Customer[] customers;
    private int numberOfCustomer;

    public Bank() {
        customers = new Customer[5];
    }

    public void addCustomer(String f, String l) {
        Customer customer = new Customer(f, l);
        customers[numberOfCustomer] = customer;
        numberOfCustomer++;
    }

    public int getNumOfCustomers() {
        return numberOfCustomer;
    }

    public Customer getCustomer(int index) {
        return customers[index];
    }
}

/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
/*
 * This class creates the program to test the banking classes.
 * It creates a new Bank, sets the Customer (with an initial balance),
 * and performs a series of transactions with the Account object.
 */
//import banking.*;
public class CreateSubclass {
    public static void main(String[] args) {
        Bank bank = new Bank();
        Customer customer;
        Account account;
//
// Create bank customers and their accounts
//
        System.out.println("Creating the customer Jane Smith.");
        bank.addCustomer("Jane", "Simms");
        customer = bank.getCustomer(0);
        System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");
        customer.setAccount(new SavingsAccount(500.00, 0.03));
        System.out.println("Creating the customer Owen Bryant.");
        bank.addCustomer("Owen", "Bryant");
        customer = bank.getCustomer(1);
        System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection.");
        customer.setAccount(new CheckingAccount(500.00));
        System.out.println("Creating the customer Tim Soley.");
        bank.addCustomer("Tim", "Soley");
        customer = bank.getCustomer(2);
        System.out.println("Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection.");
        customer.setAccount(new CheckingAccount(500.00, 500.00));
        System.out.println("Creating the customer Maria Soley.");
        bank.addCustomer("Maria", "Soley");
        customer = bank.getCustomer(3);
        System.out.println("Maria shares her Checking Account with her husband Tim.");
        customer.setAccount(bank.getCustomer(2).getAccount());
        System.out.println();
//
// Demonstrate behavior of various account types
//
// Test a standard Savings Account
        System.out.println("Retrieving the customer Jane Smith with her savings account.");
        customer = bank.getCustomer(0);
        account = customer.getAccount();
// Perform some account transactions
        System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
        System.out.println("Deposit 22.50: " + account.deposit(22.50));
        System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
        System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
// Print out the final account balance
        System.out.println("Customer [" + customer.getLastName()
                + ", " + customer.getFirstName()
                + "] has a balance of " + account.getBalance());
        System.out.println();
// Test a Checking Account w/o overdraft protection
        System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
        customer = bank.getCustomer(1);
        account = customer.getAccount();
// Perform some account transactions
        System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
        System.out.println("Deposit 22.50: " + account.deposit(22.50));
        System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
        System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
// Print out the final account balance
        System.out.println("Customer [" + customer.getLastName()
                + ", " + customer.getFirstName()
                + "] has a balance of " + account.getBalance());
        System.out.println();
// Test a Checking Account with overdraft protection
        System.out.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection.");
        customer = bank.getCustomer(2);
        account = customer.getAccount();
// Perform some account transactions
        System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
        System.out.println("Deposit 22.50: " + account.deposit(22.50));
        System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
        System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
// Print out the final account balance
        System.out.println("Customer [" + customer.getLastName()
                + ", " + customer.getFirstName()
                + "] has a balance of " + account.getBalance());
        System.out.println();
// Test a Checking Account with overdraft protection
        System.out.println("Retrieving the customer Maria Soley with her joint checking account with husband Tim.");
        customer = bank.getCustomer(3);
        account = customer.getAccount();
// Perform some account transactions
        System.out.println("Deposit 150.00: " + account.deposit(150.00));
        System.out.println("Withdraw 750.00: " + account.withdraw(750.00));
// Print out the final account balance
        System.out.println("Customer [" + customer.getLastName()
                + ", " + customer.getFirstName()
                + "] has a balance of " + account.getBalance());
    }
}
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
